import { configureStore } from '@reduxjs/toolkit'

const initialState = { value: 0 }

function counterReducer(state = initialState, action) {   
  // 检查 reducer 是否关心这个 action
  if (action.type === 'counter/increment') {
    console.log("counterReducer is called!")
    // 如果是，复制 `state`
    return {
      ...state,
      // 使用新值更新 state 副本
      value: state.value + 1
    }
  }
  // 返回原来的 state 不变
  return state
}

const store = configureStore({ reducer: counterReducer })

console.log(store.getState())

store.dispatch({ type: 'counter/increment' })

console.log(store.getState())

const selectCounterValue = state => state.value

const currentValue = selectCounterValue(store.getState())
console.log(currentValue)


export default  store